<?php

namespace tmcore\services\crud\generators;

use tmcore\services\crud\enum\TplEnum;
use tmcore\services\crud\UtilService;

class Controller extends Generate
{
    /**
     * @var string
     */
    protected string $fileType = 'php';

    /**
     * 替换变量
     */
    public function replaceContent()
    {
        $search = [
            '{NAMESPACE}',
            '{USE}',
            '{TABLE_COMMENT}',
            '{CAMEL_CLASS_NAME}',
            '{CONTENT_PHP}',
            '{EXTENDS_CONTROLLER}',
            '{AUTHOR}'
        ];

        $replace = [
            $this->getNameSpace(),
            $this->getUseContent(),
            $this->getTableCommentContent(),
            $this->getCamelClassName(),
            $this->getControllerContent(),
            $this->getExtendsControllerContent(),
            $this->getAuthorContent()
        ];

        $content = UtilService::replaceFileContent($search, $replace, $this->getStub('controller'));

        $this->setContent($content);
    }


    /**
     * 返回模板路径
     * @param string $type
     * @return mixed|string|string[]
     */
    public function getStub(string $type = 'controller')
    {
        $basePath = 'php/controller/';

        $stubs = [
            'lists' => $this->getTplPath($basePath . 'lists'),
            'detail' => $this->getTplPath($basePath . 'detail'),
            'add' => $this->getTplPath($basePath . 'add'),
            'edit' => $this->getTplPath($basePath . 'edit'),
            'delete' => $this->getTplPath($basePath . 'delete'),
            'export' => $this->getTplPath($basePath . 'export'),
            'import' => $this->getTplPath($basePath . 'import'),
            'treeAll' => $this->getTplPath($basePath . 'treeAll'),
            'controller' => $this->getTplPath($basePath . 'controller'),
        ];

        return $type ? $stubs[$type] : $stubs;
    }

    /**
     * 设置文件路径
     */
    public function setFilePath()
    {
        $name = $this->getCamelClassName() . '.' . $this->fileType;
        $this->setCrudFileName($name);

        $module_path = $this->basePath . $this->moduleName . '/controller/';
        if (!empty($this->classDir)) $module_path .= $this->classDir . '/';
        $this->setCrudModuleFilePath($module_path);

        $data_path = $this->crudDir . 'php/app/' . $this->moduleName . '/controller/';
        if (!empty($this->classDir)) $data_path .= $this->classDir . '/';
        $this->setCrudDataFilePath($data_path);
    }

    /**
     * 获取控制器内容
     * @return string|string[]
     */
    public function getControllerContent()
    {

        $search = [
            '{TABLE_COMMENT}',
            '{CAMEL_CLASS_NAME}',
            '{AUTHOR}'
        ];

        $replace = [
            $this->crudData['table_comment'],
            $this->getCamelClassName(),
            $this->getAuthorContent()
        ];

        $content = UtilService::replaceFileContent($search, $replace, $this->getStubContent(TplEnum::getControllerTpl($this->crudData)), false);

        return substr($content, 0, -1);
    }

    /**
     * 获取命名空间
     * @return string
     */
    public function getNameSpace()
    {
        if (!empty($this->classDir)) return "namespace app\\" . $this->moduleName . "\\controller\\" . $this->classDir . ';';
        return "namespace app\\" . $this->moduleName . "\\controller;";
    }

    /**
     * 获取use模板内容
     * @return string
     */
    public function getUseContent()
    {
        $tpl = "use app\\" . $this->moduleName . "\\controller\\AdminController;" . PHP_EOL;

        if (!empty($this->classDir)) {
            $tpl .= "use app\\" . $this->moduleName . "\\service\\" . $this->classDir . "\\" . $this->getCamelClassName() . "Service;" . PHP_EOL .
                "use app\\" . $this->moduleName . "\\validate\\" . $this->classDir . "\\" . $this->getCamelClassName() . "Validate;" . PHP_EOL;
        } else {
            $tpl .= "use app\\" . $this->moduleName . "\\service\\" . $this->getCamelClassName() . "Service;" . PHP_EOL .
                "use app\\" . $this->moduleName . "\\validate\\" . $this->getCamelClassName() . "Validate;" . PHP_EOL;
        }

        if ($this->crudData['is_export']) $tpl .= "use app\\admin\\validate\\file\\ExportValidate;" . PHP_EOL;

        $tpl = substr($tpl, 0, -1);

        return $tpl;
    }

    /**
     * 获取描述内容
     * @return string
     */
    public function getTableCommentContent()
    {
        if (!empty($this->crudData['table_comment'])) {
            $tpl = $this->crudData['table_comment'] . '控制器';
        } else {
            $tpl = $this->getCamelClassName() . '控制器';
        }
        return $tpl;
    }

    /**
     * 获取继承控制器
     * @return string
     */
    public function getExtendsControllerContent()
    {
        return 'AdminController';
    }

}